package Example13_3;

import Test.Rational;
import Test.Solution;
import java.net.*;
import java.io.*;
import Test.StringToArithmetic;
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;

public class Server {
    public static void main(String args[]) {
        //String [] answer ={"南非","进入世界杯了","哈哈...问题真逗!"};
        ServerSocket serverForClient=null;
        Socket socketOnServer=null;
        DataOutputStream out=null;
        DataInputStream  in=null;
        try { serverForClient = new ServerSocket(2010);
        }
        catch(IOException e1) {
            System.out.println(e1);
        }
        try{ System.out.println("等待客户呼叫");
            socketOnServer = serverForClient.accept(); //堵塞状态，除非有客户呼叫
            out=new DataOutputStream(socketOnServer.getOutputStream());
            in=new DataInputStream(socketOnServer.getInputStream());
            while (true){
                // in读取信息，堵塞状态
                int len = Integer.parseInt(in.readUTF());
                byte ctext[]=new byte[len];
                for (int i = 0;i<len;i++) {
                    String temp = in.readUTF();
                    ctext[i] = Byte.parseByte(temp);
                }
                String pro = in.readUTF();
                String s = new String(ctext);
                System.out.println("服务器收到客户的秘密消息:"+s);
                String x=s;
                MessageDigest m=MessageDigest.getInstance("MD5");
                m.update(x.getBytes("UTF8"));
                byte s0[ ]=m.digest( );
                String result="";
                for (int i=0; i<s0.length; i++){
                    result+=Integer.toHexString((0x000000ff & s0[i]) | 0xffffff00).substring(6);
                }
                System.out.println(result);
                if(!result.equals(pro)){
                    out.writeUTF("Something wrong!");
                }
                FileInputStream  f2=new FileInputStream("keykb1.dat");
                int num2=f2.available();
                byte[ ] key=new byte[num2];
                f2.read(key);
                SecretKeySpec k=new  SecretKeySpec(key,"DESede");
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.DECRYPT_MODE, k);
                byte []ptext=cp.doFinal(ctext);
                // 显示明文
                String p=new String(ptext,"UTF8");
                System.out.println("真实信息为："+p);
                int i,j = 0;
                Rational ra = new Rational();
                Solution s1 = new Solution();
                String str [] = new String[5*p.length()];
                for (i = 0;i <p.length(); i++){
                    str [i] = "";
                }
                for (i = 0; i<p.length(); i++){
                    if (p.charAt(i) == '1'&&p.charAt(i+1) == '0'){
                        str [j] += p.charAt(i);
                    }
                    else{
                        str [j] += p.charAt(i);
                        j++;
                    }
                }
                if (!str[j-1].equals("")){
                    str[j] = "";
                }
                ra = s1.evalRPN(str);
                out.writeUTF(ra.toString());
                Thread.sleep(500);
            }
        }
        catch(Exception e) {
            System.out.println("客户已断开"+e);
        }
    }
}
